<?php
namespace app\index\controller;

\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);
use app\index\Controller;
use think\Db;
use think\Session;

class Index extends Controller
{
	 use \traits\controller\Jump;
    public function __construct()
    {
    	
        parent::__construct();
//		$this->footer();
        $action = $this->request->action();
        $this->view->assign("action", $action);
        if (!method_exists($this, $action)) {
            die( $this->view->fetch($action));
        }
    }
    /*
     *获取code 
     * */
	public function getcode(){
		$appId = 'wx45aaa00915e2fb58';
		$urls = urlencode('http://jxtxzb.mgtvshop.com');
		$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$urls."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";

		header("location:".$url);
	}
    /*
	 * 首页//
	 * */
    public function index(){
    	/*微信登入*/
		//获取用户apenid
		@$code = $_GET['code'];

		vendor ( 'ThirdLogin.WxLogin' );
		$appId = 'wx45aaa00915e2fb58';
		$appKey = 'f71bf8808055e5c44cc612f7db2464c6';

		$wx = new \WxLogin($appId,$appKey,$code);
        if(!$code){
            header("location:/index/index/getcode");exit;
        }
		//得到access_token验证值
		$accessToken = $wx->getToken();

        //得到用户的openid(登陆用户的识别码)和Client_id
        $openId = $wx->getOpenId();
        $arr['openid'] = $openId;
        $user = Db::table('jx_user')->where($arr)->find();

        if($user){
            Session::set('id',$user['id']);
            Session::set('openId',$openId);
        }else{
            $user_info = $wx->getUserInfo($openId,$accessToken);
            $info['name'] = $user_info["nickname"];
            $info['openid'] = $user_info["openid"];
            $info['img'] = $user_info["headimgurl"];
            $res = Db::table('jx_user')->insert($info);
            if($res){
                $user = Db::table('jx_user')->where($arr)->find();
                Session::set('id',$user['id']);
                Session::set('openId',$user['openid']);
            }
		}

		return $this->view->fetch();
    }
    public function detail(){
    	return $this->view->fetch();
    }
    public function reward(){
    	$user_id = Session::get('id');

    	$info = Db::table('jx_user')->where('id',$user_id)->find();
    	$figure = $info['figure']?$info['figure']:0;//账户剩余元宝数

    	$this->view->assign('figure',$figure);
    	return $this->view->fetch();
    }
    /*
     *元宝充值 
     * */
    public function recharge(){
    	$figure = $_POST['figure'];
		$user_id = Session::get('id');
    	
    	$res = Db::table('jx_user')->where('id',$user_id)->setInc('figure', $figure);
    	if($res){
    			
			//充值成功之后商户号要余额增加
			
			$info = [
				'code' => '1',
				'msg' => '元宝充值成功！'
			];
			return json_encode($info);
		}else{
			$info = [
				'code' => '-1',
				'msg' => '元宝充值失败，请稍后再试！'
			];
			return json_encode($info);
		}
    }
    /*
     *元宝赠送 
     * */
    public function give(){
    	$count = $_POST['count'];
		$user_id = Session::get('id');
    	
    	$user_info = Db::table('jx_user')->where('id',$user_id)->find();
    	if($user_info['figure'] >= $count){
    		$res = Db::table('jx_user')->where('id',$user_id)->setDec('figure', $count);
    		if($res){
    			
    			//赠送成功之后商户号要增加余额
    			
    			$info = [
					'code' => '1',
					'msg' => '元宝赠送成功！'
				];
				return json_encode($info);
    		}
    	}else{
    		$info = [
				'code' => '-1',
				'msg' => '您的元宝数不足，请及时充值！'
			];
			return json_encode($info);
    	}
    }
}
